package com.desmart.renjiao.commission;

import android.text.TextUtils;
import android.util.Log;

import com.desmart.renjiao.bean.GetCommissionRessult;
import com.desmart.renjiao.bean.GetMemberResult;
import com.desmart.renjiao.bean.LoginBean;
import com.desmart.renjiao.xmlparase.ResultXmlUtils;
import com.google.gson.Gson;
import com.lemon.library.constants.PreferencesConstants;
import com.lemon.library.net.rx.RxObservableListener;
import com.lemon.library.utils.PreferencesUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class CommissionP extends Commission.CommissionP{

    @Override
    void getSapUrl() {

        String token = PreferencesUtils.getString(PreferencesConstants.login_Token);
        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\">\n" +
                "<soapenv:Header/>\n" +
                "<soapenv:Body>\n" +
                "<ser:getSapUrl>\n" +
                "<ltpaToken>" + token +
                "</ltpaToken>\n" +
                "</ser:getSapUrl>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>\n";

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.getUrl(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                try {
                    String ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    PreferencesUtils.putString(PreferencesConstants.sap_url,result);
                    String oaUrl = PreferencesUtils.getString(PreferencesConstants.oa_url);
                    if (TextUtils.isEmpty(oaUrl)){
                        getOaUrl();
                    }else{
                        getCommissionList(1,"","","");
                    }
                }catch (Exception e){

                }
            }
        });

    }

    @Override
    void getOaUrl() {

        String token = PreferencesUtils.getString(PreferencesConstants.login_Token);
        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\">\n" +
                "<soapenv:Header/>\n" +
                "<soapenv:Body>\n" +
                "<ser:getOAUrl><ltpaToken>" + token +
                "</ltpaToken>\n" +
                "</ser:getOAUrl>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>\n";

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.getUrl(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                try {
                    String ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    PreferencesUtils.putString(PreferencesConstants.oa_url,result);
                    String sapUrl = PreferencesUtils.getString(PreferencesConstants.sap_url);
                    if (TextUtils.isEmpty(sapUrl)){
                        getSapUrl();
                    }else{
                        getCommissionList(1,"","","");
                    }
                }catch (Exception e){

                }
            }
        });

    }

    @Override
    void getCommissionList(int page, String field, String order, String type) {

        String oaUrl = PreferencesUtils.getString(PreferencesConstants.oa_url);
        String sapUrl = PreferencesUtils.getString(PreferencesConstants.sap_url);

        if (TextUtils.isEmpty(oaUrl)){
            getOaUrl();
            return;
        }else if (TextUtils.isEmpty(sapUrl)){
            getSapUrl();
            return;
        }

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\">\n" +
                "<soapenv:Header/>\n" +
                "<soapenv:Body>\n" +
                "<ser:getToDoInfo><sapUrl>" + sapUrl +
                "</sapUrl><oaUrl>" + oaUrl +
                "</oaUrl>\n" +
                "<limit>20</limit>\n" +
                "<page>" + page +
                "</page>\n" +
                "<field>" + field +
                "</field>\n" +
                "<order>" + order +
                "</order>\n" +
                "<type>" + type +
                "</type>\n" +
                "</ser:getToDoInfo>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>\n";


        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.getUrl(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                try {
                    String ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    GetCommissionRessult getCommissionRessult = new Gson().fromJson(result, GetCommissionRessult.class);
                    if ("200".equals(getCommissionRessult.getCode())){
                        mView.onCommissionListResult(getCommissionRessult.getData());
                    }
                }catch (Exception e){

                }
            }
        });

    }


}
